package world.criver.imagetoicon;

import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import org.apache.commons.imaging.ImageFormat;
import org.apache.commons.imaging.ImageFormats;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.formats.ico.IcoImageParser;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;

public class conMaster {
    public TextField input_url;
    public TextField output_url;
    public CheckBox size_auto;
    public CheckBox size_256;
    public CheckBox size_128;
    public CheckBox size_64;
    public CheckBox size_32;
    public CheckBox size_16;
    public TextArea console;
    private File input;
    private File output;

    public void selectInputUrl() {
        FileChooser fc=new FileChooser();
        fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("图片","*.png","*.jpg","*.jpeg"));
        input=fc.showOpenDialog(null);
        if(input!=null){
            input_url.setText(input.getAbsolutePath());
        }
    }

    public void selectOutputUrl() {
        DirectoryChooser dc=new DirectoryChooser();
        output=dc.showDialog(null);
        if(output!=null){
            output_url.setText(output.getAbsolutePath());
        }
    }

    public void onChange() {
        if(input==null){
            app("[系统]\t错误！请选择需要转换的图片！");
            return;
        }
        if(output==null){
            app("[系统] 错误！请选择输出位置！");
            return;
        }
        if(!size_auto.isSelected() && !size_256.isSelected() && !size_128.isSelected() && !size_64.isSelected() && !size_32.isSelected() && !size_16.isSelected()){
            app("[系统] 错误！请选择需要转换的尺寸！");
            return;
        }
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        app("[系统] 开始转换-"+sf.format(new Date()));
        try{
            BufferedImage in=ImageIO.read(input);
            if((size_auto.isSelected() && in.getHeight()>=256 && in.getWidth()>=256)||(!size_auto.isSelected() && size_256.isSelected()))writeIco(in,256);
            if((size_auto.isSelected() && in.getHeight()>=128 && in.getWidth()>=128)||(!size_auto.isSelected() && size_128.isSelected()))writeIco(in,128);
            if((size_auto.isSelected() && in.getHeight()>=64 && in.getWidth()>=64)||(!size_auto.isSelected() && size_64.isSelected()))writeIco(in,64);
            if((size_auto.isSelected() && in.getHeight()>=32 && in.getWidth()>=32)||(!size_auto.isSelected() && size_32.isSelected()))writeIco(in,32);
            if((size_auto.isSelected() && in.getHeight()>=16 && in.getWidth()>=16)||(!size_auto.isSelected() && size_16.isSelected()))writeIco(in,16);
            app("[系统] 转换结束。");
        }catch (Exception e){
            app(e.getMessage());
            throw new RuntimeException(e);
        }
    }
    private void writeIco(BufferedImage in,int size) throws IOException {
        app("[系统] 开始"+size+"x"+size+"尺寸转换...");
        BufferedImage n=new BufferedImage(size,size,BufferedImage.TYPE_4BYTE_ABGR);
        Graphics g=n.getGraphics();
        g.drawImage(in,0,0,size,size,null);
//        ImageIO.write(n,"ico",new File(output.getPath().replaceAll("\\\\","/")+"/ico_"+size+"x"+size+".ico"));
        Imaging.writeImage(n,new File(output.getPath().replaceAll("\\\\","/")+"/ico_"+size+"x"+size+".ico"), ImageFormats.ICO);
        app("[系统] 完成"+size+"x"+size+"尺寸转换!");
    }
    private void app(String me){
        console.appendText(me+"\n");
        console.setScrollTop(Double.MAX_VALUE);
    }
}